package chapter8

import org.apache.spark.streaming.dstream.DStream
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.{SparkConf, SparkContext}

object Chapter8_2_2 extends App {

  val conf =  new SparkConf().setAppName(this.getClass.getName).setMaster("local[*]")

  val sc = new SparkContext(conf)

  val ssc = new StreamingContext(sc, Seconds(5))

  val lines = ssc.socketTextStream("localhost", 9999)

  private val words: DStream[String] = lines.flatMap(_.split(" "))

  words.map((_, 1)).reduceByKey(_ + _).print()

  ssc.start()

  ssc.awaitTermination()



}
